package arg

import (
	"errors"
	"fmt"
	"db2s/global"
	"reflect"
)

type inspectionMode struct {
	SlowLog reflect.Value `json:"slowLog"`
	Cpu     reflect.Value `json:"cpu"`
}

func (rc *ConfigParameter) inspectionFunctionGet() error {
	var (
		modeGarth map[string]any
		err       error
	)
	if modeGarth, err = global.StructToMapAny(getInspectionFunction()); err != nil {
		return err
	}
	for k := range rc.FirstL1.Inspection {
		if v, ok := modeGarth[k]; ok {
			l := v.(reflect.Value).Call(nil)
			if l[0].Interface() != nil {
				return l[0].Interface().(error)
			}
		}
	}
	return nil
}

type inspectionLevel struct {
}

// inspectionLevelParameter
func (rc *ConfigParameter) inspectionLevelParameter() error {
	var (
		s2 = make(map[string]any)
	)
	if r, err := global.StructToMapAny(getInspection()); err != nil {
		return err
	} else {
		for k, v := range r {
			if r1 := rc.getHoconConfigVal1(rc.FirstL1.Inspection.ToConfig(), k, reflect.TypeOf(v).String()); r1 != nil {
				s2[k] = r1
			} else {
				s2[k] = v
			}
		}
	}
	if err := global.MapSubToStructSubJsonName(s2, &rc.SecondaryL.InspectionV); err != nil {
		vlog := fmt.Sprintf("(%d) %s rule parameter init fail. json unmarshal fail error info is %s. json data is {%s}", rc.LogThreadSeq, rc.event, err)
		WLog.Error(vlog)
		err = errors.New(fmt.Sprintf("rule parameter json unmarshal fail,please check the log for details!"))
		return err
	}
	if err := rc.inspectionFunctionGet(); err != nil {
		return err
	}
	return nil
}
